package com.sxfq.loansupermarket.backsystem.modular.theTest.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.IncompleteTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.SearchTestingInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TheTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.IIncompleteTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 未完成考试
 *
 * @author py
 * @Date 2018年2月4日23:05:54
 */
@Controller
@RequestMapping("/incompleteTest")
public class IncompleteTestController extends BaseController {

    @Autowired
    IIncompleteTestService iIncompleteTestService;

    @RequestMapping("")
    public String incompleteTest(Model model) {
        return "/theTest/incompleteTest.html";
    }

    /**
     * 未完成考试列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(SearchTestingInfo query) throws Exception {
        Page<IncompleteTestInfo> page = new PageFactory<IncompleteTestInfo>().defaultPage();
        try {
            List<IncompleteTestInfo> incompleteTestInfoList = iIncompleteTestService.selectPageListByQueryGetMap(page, query);
            page.setRecords(incompleteTestInfoList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /*
    * 重新进入考试页面
    * */
    @RequestMapping(value = "/rein/{id}")
    @ResponseBody
    public String rein(@PathVariable String id) throws Exception {
        TheTestInfo theTestInfo =iIncompleteTestService.selectOneById(id);
        ShiroKit.setSessionAttr("theTestName", theTestInfo.getTheTestName());
        ShiroKit.setSessionAttr("timeLength", theTestInfo.getTimeLength());
        ShiroKit.setSessionAttr("totalScore", getDailyInterestRate(theTestInfo.getTotalScore().toString()));
        ShiroKit.setSessionAttr("numberOfQuestion", theTestInfo.getNumberOfQuestion());
        ShiroKit.setSessionAttr("scoresOfQuestion", getDailyInterestRate(theTestInfo.getScoresOfQuestion().toString()));
        ShiroKit.setSessionAttr("userName", ShiroKit.getUser().getName());
        ShiroKit.setSessionAttr("theTestId", id);
        return "/theTest/testing.html";
    }

    private String getDailyInterestRate(String dailyInterestRate) {
        if (dailyInterestRate.indexOf(".") > 0) {
            //正则表达
            dailyInterestRate = dailyInterestRate.replaceAll("0+?$", "");//去掉后面无用的零
            dailyInterestRate = dailyInterestRate.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
        }
        return dailyInterestRate;
    }
}
